package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.BaseActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.LoginActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.RegistrationModel;
import com.diudiutechnology.www.yadiudiu.R;

import org.xutils.common.Callback;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/30.
 * 提现到余额
 */

public class WithdrawalBalanceActivity  extends BaseActivity implements View.OnClickListener ,Callback.CommonCallback<String> {
    @Bind(R.id.retur)
    LinearLayout retur;
    @Bind(R.id.money)
    TextView money;//可用余额

    @Bind(R.id.withdrawal)
    TextView withdrawal;//全部提现
    @Bind(R.id.money_edit)
    EditText money_edit;//

    @Bind(R.id.amount_text)
    TextView amount_text;//抽取费用后的金额

    @Bind(R.id.commission)
    TextView commission;//抽取百分比

    @Bind(R.id.contact)
    TextView contact;//提现

    String serviceCharge="0.00";//百分比
    Context context;
    private MyProgressDialog mDialog;
    String Judge_number="0";//判断哪个图片   0、红包转入余额页面 1、红包提现
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdrawal_balance);
        ButterKnife.bind(this);
        context=this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        withdrawal.setOnClickListener(this);
        contact.setOnClickListener(this);
        processuserRedBalance();
        money_edit.addTextChangedListener(textWatcher);
    }
    private TextWatcher textWatcher = new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            if (s.length()>0){
                if (!TextUtils.isEmpty(money_edit.getText().toString().trim())){
                    double aFloat=0;
                    if (!TextUtils.isEmpty(serviceCharge)){
                       aFloat = Double.valueOf(serviceCharge);
                    }
                    double moy=Double.valueOf(money_edit.getText().toString().trim());
                    double v = moy-(moy * (aFloat / 100));
                    DecimalFormat df   = new DecimalFormat("######0.00");
                    String format = df.format(v);
                    amount_text.setText("¥"+format);
                }
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
        }
    };
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.withdrawal:
                money_edit.setText(money.getText().toString());
                break;
            case R.id.contact:
                if (TextUtils.isEmpty(money_edit.getText().toString().trim())){
                    ToastUtils.show(context, getResources().getString(R.string.Please_withdrawal_amount));
                    return;
                }
                if (Double.valueOf(money_edit.getText().toString().trim())<=0){
                    ToastUtils.show(context, getResources().getString(R.string.Please_exceeded));
                    return;
                }
                processeedCashWithdrawal();
                break;
        }
    }
    //红包转入余额页面
    private void processuserRedBalance() {
        if (Sundry.isOpenNetwork(context)) {
            mDialog = new MyProgressDialog(context);
            Judge_number="0";
            HttpClient.userRedBalance(this);
        } else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }
    //红包提现
    private void processeedCashWithdrawal() {
        if (Sundry.isOpenNetwork(context)) {
            mDialog = new MyProgressDialog(context);
            Judge_number="1";
            HttpClient.eedCashWithdrawal(money_edit.getText().toString().trim(),this);
        } else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }
    @Override
    public void onSuccess(String result) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        System.out.println("stringmd5result" + result);
        List<RegistrationModel> getnote = JsonTools.getunderlying(result);
        if (getnote != null || getnote.size() > 0) {
            String status = getnote.get(0).getStatus();
            String getMsg = getnote.get(0).getMsg();
            if (status.equals("200")) {
            if (Judge_number.equals("0")){
                String mon = getnote.get(0).getMoney();
                serviceCharge = getnote.get(0).getServiceCharge();
                money.setText(mon);
                commission.setText("实际到账(平台手续费："+serviceCharge+"%)");
            }else {
                processuserRedBalance();
                ToastUtils.show(context, getMsg);
            }
            } else if (status.equals("105")) {
                Intent intent = new Intent(context, LoginActivity.class);
                startActivity(intent);
                ToastUtils.show(context, getMsg);
            } else {
                ToastUtils.show(context, getMsg);
            }
        }
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        if (mDialog != null) {
            mDialog.dismiss();
        }

        System.out.println("stringmd5result" + ex.getMessage().toString());
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onCancelled(CancelledException cex) {
        if (mDialog != null) {
            mDialog.dismiss();
        }

        System.out.println("stringmd5result" + cex.getMessage().toString());
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onFinished() {

    }
}

